<?php
	App::import('Model','Client');
	App::import('Security');

	class ClientTestCase extends CakeTestCase
	{
		var $fixtures = array('app.user', 'app.portfolio', 'app.currency');
		var $password = 'TestPassword10';
		
		function createNewUser()
		{		
			$newClient = Array(
            		'username' => 'NewUser',
					'email' => 'test@test.com',
					'confirmEmail' => 'test@test.com',
            		'password' => Security::hash(Configure::read('Security.salt') . $this->password),
        			'confirmPassword' => $this->password);
			
			return $newClient;
		}

		function testValidRegististration()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();        	
			$result = $this->User->register($newUser);

			$this->assertTrue($result);
			$this->assertTrue($result['Portfolio']);
		}

		function testFakeUserIdExploit()
		{
			$this->User =& ClassRegistry::init('User');
			
			$testUser = $this->User->findByUsername('TestUser');
			
			//Create a user data set with the id of an existing user
			$newUser = $this->createNewUser();
			$newUser['id'] = $testUser['User']['id'];
		    
			//Register the new user
			$this->User->register($newUser);
			$newUser = $this->User->findByUsername('NewUser');

			$this->assertNotEqual($testUser['User']['id'], $newUser['User']['id']);
		}

		function testRegistrationWithoutUsername()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			unset($newUser['username']);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegistrationWithEmptyUsername()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['username'] = '';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegistrationWithInvalidUsername()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['username'] = '#InvalidName$';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithDuplicateUsername()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['username'] = 'TestUser';
        	
			$result = $this->User->register($newUser);
			$this->assertFalse($result);
		}
		
		function testRegististrationWithUsernameOverlength()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['username'] = 'ReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongUserName';
        	
			$result = $this->User->register($newUser);
			$this->assertFalse($result);
		}
		
		function testRegististrationWithInvalidEmail()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['email'] = 'InvalidEmail';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}		
		
		function testRegististrationWithOverlengthEmail()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['email'] = 'ReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReally@LongEmail.com';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithEmptyEmail()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['email'] = '';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithMissingEmail()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			unset($newUser['email']);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithEmailUnconfirmed()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['confirmEmail'] = 'NotTheEmail@test.com';
        	
			$result = $this->User->register($newUser);
			$this->assertFalse($result);
		}
		
	function testRegististrationWithEmptyEmailConfirm()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['confirmEmail'] = '';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithMissingEmailConfirm()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			unset($newUser['confirmEmail']);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithInvalidEmailConfirm()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['confirmPassword'] = 'SomeString';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}

		function testRegististrationWithEmptyPassword()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['password'] = '';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithMissingPassword()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			unset($newUser['password']);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithPasswordUnconfirmed()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['confirmPassword'] = 'NotTheSamePassword';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithEmptyPasswordConfirm()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			$newUser['confirmPassword'] = '';
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithMissingPasswordConfirm()
		{
			$this->User =& ClassRegistry::init('User');
			
			$newUser = $this->createNewUser();
			unset($newUser['confirmPassword']);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testRegististrationWithWeakPassword()
		{
			$this->User =& ClassRegistry::init('User');
			
			$weakPassword = 'aaaaa';
			$newUser = $this->createNewUser();
			$newUser['confirmPassword'] = $weakPassword;
			$newUser['password'] = Security::hash(Configure::read('Security.salt') . $weakPassword);
        	
			$result = $this->User->register($newUser);	
			$this->assertFalse($result);
		}
		
		function testDuplicateUserName()
		{
			$this->User =& ClassRegistry::init('User');
			$result = $this->User->isUsernameUnique('TestUser');
			
			$this->assertFalse($result);
		}
		
		function testUniqueUserName()
		{
			$this->User =& ClassRegistry::init('User');
			$result = $this->User->isUsernameUnique('JohnSmith');
			
			$this->assertTrue($result);
		}
		
		function testConfirmPasswordWithMatchingPasswords()
		{
			$this->User =& ClassRegistry::init('User');
			
			$User = $this->User->findByUsername('TestUser');
			$this->User->id = $User['User']['id'];
			$this->User->read();
			$this->User->set(array('confirmPassword' => $this->password));
			$result = $this->User->confirmPassword($User['User']);
			
			$this->assertTrue($result);
		}
		
		function testConfirmPasswordWithNonmatchingPasswords()
		{
			$this->User =& ClassRegistry::init('User');
			
			$User = $this->User->findByUsername('TestUser');
			$this->User->id = $User['User']['id'];
			$this->User->read();
			$this->User->set(array('confirmPassword' => 'NotThePassword'));
			$result = $this->User->confirmPassword($User['User']);
			
			$this->assertFalse($result);
		}

	}
?>